home *** CD-ROM | disk | FTP | other *** search
/ Games of Daze / Infomagic - Games of Daze (Summer 1995) (Disc 1 of 2).iso / djgpp / libsrc / c / gen / ctime.c < prev    next >
Encoding:
C/C++ Source or Header  |  1994-07-18  |  35.1 KB  |  1,380 lines

  1. /* This is file CTIME.C */
  2. /* This file may have been modified by DJ Delorie (Jan 1991).  If so,
  3. ** these modifications are Coyright (C) 1993 DJ Delorie, 24 Kirsten Ave,
  4. ** Rochester NH, 03867-2954, USA.
  5. */
  6.  
  7. /*
  8.  * Copyright (c) 1987, 1989 Regents of the University of California.
  9.  * All rights reserved.
  10.  *
  11.  * This code is derived from software contributed to Berkeley by
  12.  * Arthur David Olson of the National Cancer Institute.
  13.  *
  14.  * Redistribution and use in source and binary forms are permitted provided
  15.  * that: (1) source distributions retain this entire copyright notice and
  16.  * comment, and (2) distributions including binaries display the following
  17.  * acknowledgement:  ``This product includes software developed by the
  18.  * University of California, Berkeley and its contributors'' in the
  19.  * documentation or other materials provided with the distribution and in
  20.  * all advertising materials mentioning features or use of this software.
  21.  * Neither the name of the University nor the names of its contributors may
  22.  * be used to endorse or promote products derived from this software without
  23.  * specific prior written permission.
  24.  * THIS SOFTWARE IS PROVIDED ``AS IS'' AND WITHOUT ANY EXPRESS OR IMPLIED
  25.  * WARRANTIES, INCLUDING, WITHOUT LIMITATION, THE IMPLIED WARRANTIES OF
  26.  * MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE.
  27.  */
  28.  
  29. #if defined(LIBC_SCCS) && !defined(lint)
  30. static char sccsid[] = "@(#)ctime.c    5.23 (Berkeley) 6/22/90";
  31. #endif /* LIBC_SCCS and not lint */
  32.  
  33. /*
  34. ** Leap second handling from Bradley White (bww@k.gp.cs.cmu.edu).
  35. ** POSIX-style TZ environment variable handling from Guy Harris
  36. ** (guy@auspex.com).
  37. */
  38.  
  39. /*LINTLIBRARY*/
  40.  
  41. #include <sys/param.h>
  42. #include <fcntl.h>
  43. #include <time.h>
  44. #include <tzfile.h>
  45. #include <string.h>
  46. #include <ctype.h>
  47. #include <stdio.h>
  48.  
  49. #ifdef __STDC__
  50. #include <stdlib.h>
  51.  
  52. #define P(s)        s
  53. #define alloc_size_t    size_t
  54. #define qsort_size_t    size_t
  55. #define fread_size_t    size_t
  56. #define fwrite_size_t    size_t
  57.  
  58. #else /* !defined __STDC__ */
  59.  
  60. #define P(s)        ()
  61. #define const
  62. #define volatile
  63.  
  64. typedef char *        genericptr_t;
  65. typedef unsigned    alloc_size_t;
  66. typedef int        qsort_size_t;
  67. typedef int        fread_size_t;
  68. typedef int        fwrite_size_t;
  69.  
  70. extern char *    calloc();
  71. extern char *    malloc();
  72. extern char *    realloc();
  73. extern char *    getenv();
  74.  
  75. #endif /* !defined __STDC__ */
  76.  
  77. extern unsigned long    time();
  78.  
  79. #define ACCESS_MODE    O_RDONLY
  80. #define OPEN_MODE    O_RDONLY
  81.  
  82. #ifndef WILDABBR
  83. /*
  84. ** Someone might make incorrect use of a time zone abbreviation:
  85. **    1.    They might reference tzname[0] before calling tzset (explicitly
  86. **         or implicitly).
  87. **    2.    They might reference tzname[1] before calling tzset (explicitly
  88. **         or implicitly).
  89. **    3.    They might reference tzname[1] after setting to a time zone
  90. **        in which Daylight Saving Time is never observed.
  91. **    4.    They might reference tzname[0] after setting to a time zone
  92. **        in which Standard Time is never observed.
  93. **    5.    They might reference tm.TM_ZONE after calling offtime.
  94. ** What's best to do in the above cases is open to debate;
  95. ** for now, we just set things up so that in any of the five cases
  96. ** WILDABBR is used.  Another possibility:  initialize tzname[0] to the
  97. ** string "tzname[0] used before set", and similarly for the other cases.
  98. ** And another:  initialize tzname[0] to "ERA", with an explanation in the
  99. ** manual page of what this "time zone abbreviation" means (doing this so
  100. ** that tzname[0] has the "normal" length of three characters).
  101. */
  102. #define WILDABBR    "   "
  103. #endif /* !defined WILDABBR */
  104.  
  105. #ifndef TRUE
  106. #define TRUE        1
  107. #define FALSE        0
  108. #endif /* !defined TRUE */
  109.  
  110. static const char GMT[] = "GMT";
  111.  
  112. struct ttinfo {                /* time type information */
  113.     long        tt_gmtoff;    /* GMT offset in seconds */
  114.     int        tt_isdst;    /* used to set tm_isdst */
  115.     int        tt_abbrind;    /* abbreviation list index */
  116.     int        tt_ttisstd;    /* TRUE if transition is std time */
  117. };
  118.  
  119. struct lsinfo {                /* leap second information */
  120.     time_t        ls_trans;    /* transition time */
  121.     long        ls_corr;    /* correction to apply */
  122. };
  123.  
  124. struct state {
  125.     int        leapcnt;
  126.     int        timecnt;
  127.     int        typecnt;
  128.     int        charcnt;
  129.     time_t        ats[TZ_MAX_TIMES];
  130.     unsigned char    types[TZ_MAX_TIMES];
  131.     struct ttinfo    ttis[TZ_MAX_TYPES];
  132.     char        chars[(TZ_MAX_CHARS + 1 > sizeof GMT) ?
  133.                 TZ_MAX_CHARS + 1 : sizeof GMT];
  134.     struct lsinfo    lsis[TZ_MAX_LEAPS];
  135. };
  136.  
  137. struct rule {
  138.     int        r_type;        /* type of rule--see below */
  139.     int        r_day;        /* day number of rule */
  140.     int        r_week;        /* week number of rule */
  141.     int        r_mon;        /* month number of rule */
  142.     long        r_time;        /* transition time of rule */
  143. };
  144.  
  145. #define    JULIAN_DAY        0    /* Jn - Julian day */
  146. #define    DAY_OF_YEAR        1    /* n - day of year */
  147. #define    MONTH_NTH_DAY_OF_WEEK    2    /* Mm.n.d - month, week, day of week */
  148.  
  149. /*
  150. ** Prototypes for static functions.
  151. */
  152.  
  153. static long        detzcode P((const char * codep));
  154. static const char *    getzname P((const char * strp));
  155. static const char *    getnum P((const char * strp, int * nump, int min,
  156.                 int max));
  157. static const char *    getsecs P((const char * strp, long * secsp));
  158. static const char *    getoffset P((const char * strp, long * offsetp));
  159. static const char *    getrule P((const char * strp, struct rule * rulep));
  160. static void        gmtload P((struct state * sp));
  161. static void        gmtsub P((const time_t * timep, long offset,
  162.                 struct tm * tmp));
  163. static void        localsub P((const time_t * timep, long offset,
  164.                 struct tm * tmp));
  165. static void        normalize P((int * tensptr, int * unitsptr, int base));
  166. static void        settzname P((void));
  167. static time_t        time1 P((struct tm * tmp, void (* funcp)(),
  168.                 long offset));
  169. static time_t        time2 P((struct tm *tmp, void (* funcp)(),
  170.                 long offset, int * okayp));
  171. static void        timesub P((const time_t * timep, long offset,
  172.                 const struct state * sp, struct tm * tmp));
  173. static int        tmcomp P((const struct tm * atmp,
  174.                 const struct tm * btmp));
  175. static time_t        transtime P((time_t janfirst, int year,
  176.                 const struct rule * rulep, long offset));
  177. static int        tzload P((const char * name, struct state * sp));
  178. static int        tzparse P((const char * name, struct state * sp,
  179.                 int lastditch));
  180.  
  181. #ifdef ALL_STATE
  182. static struct state *    lclptr;
  183. static struct state *    gmtptr;
  184. #endif /* defined ALL_STATE */
  185.  
  186. #ifndef ALL_STATE
  187. static struct state    lclmem;
  188. static struct state    gmtmem;
  189. #define lclptr        (&lclmem)
  190. #define gmtptr        (&gmtmem)
  191. #endif /* State Farm */
  192.  
  193. static int        lcl_is_set;
  194. static int        gmt_is_set;
  195.  
  196. char *            tzname[2] = {
  197.     WILDABBR,
  198.     WILDABBR
  199. };
  200.  
  201. #ifdef USG_COMPAT
  202. time_t            timezone = 0;
  203. int            daylight = 0;
  204. #endif /* defined USG_COMPAT */
  205.  
  206. #ifdef ALTZONE
  207. time_t            altzone = 0;
  208. #endif /* defined ALTZONE */
  209.  
  210. static long
  211. detzcode(codep)
  212. const char * const    codep;
  213. {
  214.     register long    result;
  215.     register int    i;
  216.  
  217.     result = 0;
  218.     for (i = 0; i < 4; ++i)
  219.         result = (result << 8) | (codep[i] & 0xff);
  220.     return result;
  221. }
  222.  
  223. static void
  224. settzname()
  225. {
  226.     register const struct state * const    sp = lclptr;
  227.     register int                i;
  228.  
  229.     tzname[0] = WILDABBR;
  230.     tzname[1] = WILDABBR;
  231. #ifdef USG_COMPAT
  232.     daylight = 0;
  233.     timezone = 0;
  234. #endif /* defined USG_COMPAT */
  235. #ifdef ALTZONE
  236.     altzone = 0;
  237. #endif /* defined ALTZONE */
  238. #ifdef ALL_STATE
  239.     if (sp == NULL) {
  240.         tzname[0] = tzname[1] = GMT;
  241.         return;
  242.     }
  243. #endif /* defined ALL_STATE */
  244.     for (i = 0; i < sp->typecnt; ++i) {
  245.         register const struct ttinfo * const    ttisp = &sp->ttis[i];
  246.  
  247.         tzname[ttisp->tt_isdst] =
  248.             (char *) &sp->chars[ttisp->tt_abbrind];
  249. #ifdef USG_COMPAT
  250.         if (ttisp->tt_isdst)
  251.             daylight = 1;
  252.         if (i == 0 || !ttisp->tt_isdst)
  253.             timezone = -(ttisp->tt_gmtoff);
  254. #endif /* defined USG_COMPAT */
  255. #ifdef ALTZONE
  256.         if (i == 0 || ttisp->tt_isdst)
  257.             altzone = -(ttisp->tt_gmtoff);
  258. #endif /* defined ALTZONE */
  259.     }
  260.     /*
  261.     ** And to get the latest zone names into tzname. . .
  262.     */
  263.     for (i = 0; i < sp->timecnt; ++i) {
  264.         register const struct ttinfo * const    ttisp =
  265.                             &sp->ttis[sp->types[i]];
  266.  
  267.         tzname[ttisp->tt_isdst] =
  268.             (char *) &sp->chars[ttisp->tt_abbrind];
  269.     }
  270. }
  271.  
  272. static int
  273. tzload(name, sp)
  274. register const char *        name;
  275. register struct state * const    sp;
  276. {
  277.     register const char *    p;
  278.     register int        i;
  279.     register int        fid;
  280.  
  281.     if (name == NULL && (name = TZDEFAULT) == NULL)
  282.         return -1;
  283.     {
  284.         char        fullname[FILENAME_MAX + 1];
  285.  
  286.         if (name[0] == ':')
  287.             ++name;
  288.         if (name[0] != '/') {
  289.             if ((p = TZDIR) == NULL)
  290.                 return -1;
  291.             if ((strlen(p) + strlen(name) + 1) >= sizeof fullname)
  292.                 return -1;
  293.             (void) strcpy(fullname, p);
  294.             (void) strcat(fullname, "/");
  295.             (void) strcat(fullname, name);
  296.             name = fullname;
  297.         }
  298.         if ((fid = open(name, OPEN_MODE)) == -1)
  299.             return -1;
  300.     }
  301.     {
  302.         register const struct tzhead *    tzhp;
  303.         char                buf[sizeof *sp + sizeof *tzhp];
  304.         int                ttisstdcnt;
  305.  
  306.         i = read(fid, buf, sizeof buf);
  307.         if (close(fid) != 0 || i < sizeof *tzhp)
  308.             return -1;
  309.         tzhp = (struct tzhead *) buf;
  310.         ttisstdcnt = (int) detzcode(tzhp->tzh_ttisstdcnt);
  311.         sp->leapcnt = (int) detzcode(tzhp->tzh_leapcnt);
  312.         sp->timecnt = (int) detzcode(tzhp->tzh_timecnt);
  313.         sp->typecnt = (int) detzcode(tzhp->tzh_typecnt);
  314.         sp->charcnt = (int) detzcode(tzhp->tzh_charcnt);
  315.         if (sp->leapcnt < 0 || sp->leapcnt > TZ_MAX_LEAPS ||
  316.             sp->typecnt <= 0 || sp->typecnt > TZ_MAX_TYPES ||
  317.             sp->timecnt < 0 || sp->timecnt > TZ_MAX_TIMES ||
  318.             sp->charcnt < 0 || sp->charcnt > TZ_MAX_CHARS ||
  319.             (ttisstdcnt != sp->typecnt && ttisstdcnt != 0))
  320.                 return -1;
  321.         if (i < sizeof *tzhp +
  322.             sp->timecnt * (4 + sizeof (char)) +
  323.             sp->typecnt * (4 + 2 * sizeof (char)) +
  324.             sp->charcnt * sizeof (char) +
  325.             sp->leapcnt * 2 * 4 +
  326.             ttisstdcnt * sizeof (char))
  327.                 return -1;
  328.         p = buf + sizeof *tzhp;
  329.         for (i = 0; i < sp->timecnt; ++i) {
  330.             sp->ats[i] = detzcode(p);
  331.             p += 4;
  332.         }
  333.         for (i = 0; i < sp->timecnt; ++i) {
  334.             sp->types[i] = (unsigned char) *p++;
  335.             if (sp->types[i] >= sp->typecnt)
  336.                 return -1;
  337.         }
  338.         for (i = 0; i < sp->typecnt; ++i) {
  339.             register struct ttinfo *    ttisp;
  340.  
  341.             ttisp = &sp->ttis[i];
  342.             ttisp->tt_gmtoff = detzcode(p);
  343.             p += 4;
  344.             ttisp->tt_isdst = (unsigned char) *p++;
  345.             if (ttisp->tt_isdst != 0 && ttisp->tt_isdst != 1)
  346.                 return -1;
  347.             ttisp->tt_abbrind = (unsigned char) *p++;
  348.             if (ttisp->tt_abbrind < 0 ||
  349.                 ttisp->tt_abbrind > sp->charcnt)
  350.                     return -1;
  351.         }
  352.         for (i = 0; i < sp->charcnt; ++i)
  353.             sp->chars[i] = *p++;
  354.         sp->chars[i] = '\0';    /* ensure '\0' at end */
  355.         for (i = 0; i < sp->leapcnt; ++i) {
  356.             register struct lsinfo *    lsisp;
  357.  
  358.             lsisp = &sp->lsis[i];
  359.             lsisp->ls_trans = detzcode(p);
  360.             p += 4;
  361.             lsisp->ls_corr = detzcode(p);
  362.             p += 4;
  363.         }
  364.         for (i = 0; i < sp->typecnt; ++i) {
  365.             register struct ttinfo *    ttisp;
  366.  
  367.             ttisp = &sp->ttis[i];
  368.             if (ttisstdcnt == 0)
  369.                 ttisp->tt_ttisstd = FALSE;
  370.             else {
  371.                 ttisp->tt_ttisstd = *p++;
  372.                 if (ttisp->tt_ttisstd != TRUE &&
  373.                     ttisp->tt_ttisstd != FALSE)
  374.                         return -1;
  375.             }
  376.         }
  377.     }
  378.     return 0;
  379. }
  380.  
  381. static const int    mon_lengths[2][MONSPERYEAR] = {
  382.     31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31,
  383.     31, 29, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31
  384. };
  385.  
  386. static const int    year_lengths[2] = {
  387.     DAYSPERNYEAR, DAYSPERLYEAR
  388. };
  389.  
  390. /*
  391. ** Given a pointer into a time zone string, scan until a character that is not
  392. ** a valid character in a zone name is found.  Return a pointer to that
  393. ** character.
  394. */
  395.  
  396. static const char *
  397. getzname(strp)
  398. register const char *    strp;
  399. {
  400.     register char    c;
  401.  
  402.     while ((c = *strp) != '\0' && !isdigit(c) && c != ',' && c != '-' &&
  403.         c != '+')
  404.             ++strp;
  405.     return strp;
  406. }
  407.  
  408. /*
  409. ** Given a pointer into a time zone string, extract a number from that string.
  410. ** Check that the number is within a specified range; if it is not, return
  411. ** NULL.
  412. ** Otherwise, return a pointer to the first character not part of the number.
  413. */
  414.  
  415. static const char *
  416. getnum(strp, nump, min, max)
  417. register const char *    strp;
  418. int * const        nump;
  419. const int        min;
  420. const int        max;
  421. {
  422.     register char    c;
  423.     register int    num;
  424.  
  425.     if (strp == NULL || !isdigit(*strp))
  426.         return NULL;
  427.     num = 0;
  428.     while ((c = *strp) != '\0' && isdigit(c)) {
  429.         num = num * 10 + (c - '0');
  430.         if (num > max)
  431.             return NULL;    /* illegal value */
  432.         ++strp;
  433.     }
  434.     if (num < min)
  435.         return NULL;        /* illegal value */
  436.     *nump = num;
  437.     return strp;
  438. }
  439.  
  440. /*
  441. ** Given a pointer into a time zone string, extract a number of seconds,
  442. ** in hh[:mm[:ss]] form, from the string.
  443. ** If any error occurs, return NULL.
  444. ** Otherwise, return a pointer to the first character not part of the number
  445. ** of seconds.
  446. */
  447.  
  448. static const char *
  449. getsecs(strp, secsp)
  450. register const char *    strp;
  451. long * const        secsp;
  452. {
  453.     int    num;
  454.  
  455.     strp = getnum(strp, &num, 0, HOURSPERDAY);
  456.     if (strp == NULL)
  457.         return NULL;
  458.     *secsp = num * SECSPERHOUR;
  459.     if (*strp == ':') {
  460.         ++strp;
  461.         strp = getnum(strp, &num, 0, MINSPERHOUR - 1);
  462.         if (strp == NULL)
  463.             return NULL;
  464.         *secsp += num * SECSPERMIN;
  465.         if (*strp == ':') {
  466.             ++strp;
  467.             strp = getnum(strp, &num, 0, SECSPERMIN - 1);
  468.             if (strp == NULL)
  469.                 return NULL;
  470.             *secsp += num;
  471.         }
  472.     }
  473.     return strp;
  474. }
  475.  
  476. /*
  477. ** Given a pointer into a time zone string, extract an offset, in
  478. ** [+-]hh[:mm[:ss]] form, from the string.
  479. ** If any error occurs, return NULL.
  480. ** Otherwise, return a pointer to the first character not part of the time.
  481. */
  482.  
  483. static const char *
  484. getoffset(strp, offsetp)
  485. register const char *    strp;
  486. long * const        offsetp;
  487. {
  488.     register int    neg;
  489.  
  490.     if (*strp == '-') {
  491.         neg = 1;
  492.         ++strp;
  493.     } else if (isdigit(*strp) || *strp++ == '+')
  494.         neg = 0;
  495.     else    return NULL;        /* illegal offset */
  496.     strp = getsecs(strp, offsetp);
  497.     if (strp == NULL)
  498.         return NULL;        /* illegal time */
  499.     if (neg)
  500.         *offsetp = -*offsetp;
  501.     return strp;
  502. }
  503.  
  504. /*
  505. ** Given a pointer into a time zone string, extract a rule in the form
  506. ** date[/time].  See POSIX section 8 for the format of "date" and "time".
  507. ** If a valid rule is not found, return NULL.
  508. ** Otherwise, return a pointer to the first character not part of the rule.
  509. */
  510.  
  511. static const char *
  512. getrule(strp, rulep)
  513. const char *            strp;
  514. register struct rule * const    rulep;
  515. {
  516.     if (*strp == 'J') {
  517.         /*
  518.         ** Julian day.
  519.         */
  520.         rulep->r_type = JULIAN_DAY;
  521.         ++strp;
  522.         strp = getnum(strp, &rulep->r_day, 1, DAYSPERNYEAR);
  523.     } else if (*strp == 'M') {
  524.         /*
  525.         ** Month, week, day.
  526.         */
  527.         rulep->r_type = MONTH_NTH_DAY_OF_WEEK;
  528.         ++strp;
  529.         strp = getnum(strp, &rulep->r_mon, 1, MONSPERYEAR);
  530.         if (strp == NULL)
  531.             return NULL;
  532.         if (*strp++ != '.')
  533.             return NULL;
  534.         strp = getnum(strp, &rulep->r_week, 1, 5);
  535.         if (strp == NULL)
  536.             return NULL;
  537.         if (*strp++ != '.')
  538.             return NULL;
  539.         strp = getnum(strp, &rulep->r_day, 0, DAYSPERWEEK - 1);
  540.     } else if (isdigit(*strp)) {
  541.         /*
  542.         ** Day of year.
  543.         */
  544.         rulep->r_type = DAY_OF_YEAR;
  545.         strp = getnum(strp, &rulep->r_day, 0, DAYSPERLYEAR - 1);
  546.     } else    return NULL;        /* invalid format */
  547.     if (strp == NULL)
  548.         return NULL;
  549.     if (*strp == '/') {
  550.         /*
  551.         ** Time specified.
  552.         */
  553.         ++strp;
  554.         strp = getsecs(strp, &rulep->r_time);
  555.     } else    rulep->r_time = 2 * SECSPERHOUR;    /* default = 2:00:00 */
  556.     return strp;
  557. }
  558.  
  559. /*
  560. ** Given the Epoch-relative time of January 1, 00:00:00 GMT, in a year, the
  561. ** year, a rule, and the offset from GMT at the time that rule takes effect,
  562. ** calculate the Epoch-relative time that rule takes effect.
  563. */
  564.  
  565. static time_t
  566. transtime(janfirst, year, rulep, offset)
  567. const time_t                janfirst;
  568. const int                year;
  569. register const struct rule * const    rulep;
  570. const long                offset;
  571. {
  572.   register int    leapyear;
  573.   register time_t    value;
  574.   register int    i;
  575.   int        d, m1, yy0, yy1, yy2, dow;
  576.  
  577.   leapyear = isleap(year);
  578.   switch (rulep->r_type) {
  579.  
  580.   case JULIAN_DAY:
  581.     /*
  582.      ** Jn - Julian day, 1 == January 1, 60 == March 1 even in leap
  583.      ** years.
  584.      ** In non-leap years, or if the day number is 59 or less, just
  585.      ** add SECSPERDAY times the day number-1 to the time of
  586.      ** January 1, midnight, to get the day.
  587.      */
  588.     value = janfirst + (rulep->r_day - 1) * SECSPERDAY;
  589.     if (leapyear && rulep->r_day >= 60)
  590.       value += SECSPERDAY;
  591.     break;
  592.  
  593.   case DAY_OF_YEAR:
  594.     /*
  595.      ** n - day of year.
  596.      ** Just add SECSPERDAY times the day number to the time of
  597.      ** January 1, midnight, to get the day.
  598.      */
  599.     value = janfirst + rulep->r_day * SECSPERDAY;
  600.     break;
  601.  
  602.   case MONTH_NTH_DAY_OF_WEEK:
  603.     /*
  604.      ** Mm.n.d - nth "dth day" of month m.
  605.      */
  606.     value = janfirst;
  607.     for (i = 0; i < rulep->r_mon - 1; ++i)
  608.       value += mon_lengths[leapyear][i] * SECSPERDAY;
  609.  
  610.     /*
  611.      ** Use Zeller's Congruence to get day-of-week of first day of
  612.      ** month.
  613.      */
  614.     m1 = (rulep->r_mon + 9) % 12 + 1;
  615.     yy0 = (rulep->r_mon <= 2) ? (year - 1) : year;
  616.     yy1 = yy0 / 100;
  617.     yy2 = yy0 % 100;
  618.     dow = ((26 * m1 - 2) / 10 +
  619.        1 + yy2 + yy2 / 4 + yy1 / 4 - 2 * yy1) % 7;
  620.     if (dow < 0)
  621.       dow += DAYSPERWEEK;
  622.  
  623.     /*
  624.      ** "dow" is the day-of-week of the first day of the month.  Get
  625.      ** the day-of-month (zero-origin) of the first "dow" day of the
  626.      ** month.
  627.      */
  628.     d = rulep->r_day - dow;
  629.     if (d < 0)
  630.       d += DAYSPERWEEK;
  631.     for (i = 1; i < rulep->r_week; ++i) {
  632.       if (d + DAYSPERWEEK >=
  633.       mon_lengths[leapyear][rulep->r_mon - 1])
  634.     break;
  635.       d += DAYSPERWEEK;
  636.     }
  637.  
  638.     /*
  639.      ** "d" is the day-of-month (zero-origin) of the day we want.
  640.      */
  641.     value += d * SECSPERDAY;
  642.     break;
  643.   }
  644.  
  645.   /*
  646.    ** "value" is the Epoch-relative time of 00:00:00 GMT on the day in
  647.    ** question.  To get the Epoch-relative time of the specified local
  648.    ** time on that day, add the transition time and the current offset
  649.    ** from GMT.
  650.    */
  651.   return value + rulep->r_time + offset;
  652. }
  653.  
  654. /*
  655. ** Given a POSIX section 8-style TZ string, fill in the rule tables as
  656. ** appropriate.
  657. */
  658.  
  659. static int
  660. tzparse(name, sp, lastditch)
  661. const char *            name;
  662. register struct state * const    sp;
  663. const int            lastditch;
  664. {
  665.   const char *            stdname;
  666.   const char *            dstname;
  667.   int                stdlen;
  668.   int                dstlen;
  669.   long                stdoffset;
  670.   long                dstoffset;
  671.   register time_t *        atp;
  672.   register unsigned char *    typep;
  673.   register char *            cp;
  674.   register int            load_result;
  675.  
  676.   stdname = name;
  677.   if (lastditch) {
  678.     stdlen = strlen(name);    /* length of standard zone name */
  679.     name += stdlen;
  680.     if (stdlen >= sizeof sp->chars)
  681.       stdlen = (sizeof sp->chars) - 1;
  682.   } else {
  683.     name = getzname(name);
  684.     stdlen = name - stdname;
  685.     if (stdlen < 3)
  686.       return -1;
  687.   }
  688.   if (*name == '\0')
  689.     return -1;
  690.   else {
  691.     name = getoffset(name, &stdoffset);
  692.     if (name == NULL)
  693.       return -1;
  694.   }
  695.   load_result = tzload(TZDEFRULES, sp);
  696.   if (load_result != 0)
  697.     sp->leapcnt = 0;        /* so, we're off a little */
  698.   if (*name != '\0') {
  699.     dstname = name;
  700.     name = getzname(name);
  701.     dstlen = name - dstname;    /* length of DST zone name */
  702.     if (dstlen < 3)
  703.       return -1;
  704.     if (*name != '\0' && *name != ',' && *name != ';') {
  705.       name = getoffset(name, &dstoffset);
  706.       if (name == NULL)
  707.     return -1;
  708.     } else    dstoffset = stdoffset - SECSPERHOUR;
  709.     if (*name == ',' || *name == ';') {
  710.       struct rule    start;
  711.       struct rule    end;
  712.       register int    year;
  713.       register time_t    janfirst;
  714.       time_t        starttime;
  715.       time_t        endtime;
  716.  
  717.       ++name;
  718.       if ((name = getrule(name, &start)) == NULL)
  719.     return -1;
  720.       if (*name++ != ',')
  721.     return -1;
  722.       if ((name = getrule(name, &end)) == NULL)
  723.     return -1;
  724.       if (*name != '\0')
  725.     return -1;
  726.       sp->typecnt = 2;        /* standard time and DST */
  727.       /*
  728.        ** Two transitions per year, from EPOCH_YEAR to 2037.
  729.        */
  730.       sp->timecnt = 2 * (2037 - EPOCH_YEAR + 1);
  731.       if (sp->timecnt > TZ_MAX_TIMES)
  732.     return -1;
  733.       sp->ttis[0].tt_gmtoff = -dstoffset;
  734.       sp->ttis[0].tt_isdst = 1;
  735.       sp->ttis[0].tt_abbrind = stdlen + 1;
  736.       sp->ttis[1].tt_gmtoff = -stdoffset;
  737.       sp->ttis[1].tt_isdst = 0;
  738.       sp->ttis[1].tt_abbrind = 0;
  739.       atp = sp->ats;
  740.       typep = sp->types;
  741.       janfirst = 0;
  742.       for (year = EPOCH_YEAR; year <= 2037; ++year) {
  743.     starttime = transtime(janfirst, year, &start,
  744.                   stdoffset);
  745.     endtime = transtime(janfirst, year, &end,
  746.                 dstoffset);
  747.     if (starttime > endtime) {
  748.       *atp++ = endtime;
  749.       *typep++ = 1;        /* DST ends */
  750.       *atp++ = starttime;
  751.       *typep++ = 0;        /* DST begins */
  752.     } else {
  753.       *atp++ = starttime;
  754.       *typep++ = 0;        /* DST begins */
  755.       *atp++ = endtime;
  756.       *typep++ = 1;        /* DST ends */
  757.     }
  758.     janfirst +=
  759.       year_lengths[isleap(year)] * SECSPERDAY;
  760.       }
  761.     } else {
  762.       int        sawstd;
  763.       int        sawdst;
  764.       long        stdfix;
  765.       long        dstfix;
  766.       long        oldfix;
  767.       int        isdst;
  768.       register int    i;
  769.  
  770.       if (*name != '\0')
  771.     return -1;
  772.       if (load_result != 0)
  773.     return -1;
  774.       /*
  775.        ** Compute the difference between the real and
  776.        ** prototype standard and summer time offsets
  777.        ** from GMT, and put the real standard and summer
  778.        ** time offsets into the rules in place of the
  779.        ** prototype offsets.
  780.        */
  781.       sawstd = FALSE;
  782.       sawdst = FALSE;
  783.       stdfix = 0;
  784.       dstfix = 0;
  785.       for (i = 0; i < sp->typecnt; ++i) {
  786.     if (sp->ttis[i].tt_isdst) {
  787.       oldfix = dstfix;
  788.       dstfix =
  789.         sp->ttis[i].tt_gmtoff + dstoffset;
  790.       if (sawdst && (oldfix != dstfix))
  791.         return -1;
  792.       sp->ttis[i].tt_gmtoff = -dstoffset;
  793.       sp->ttis[i].tt_abbrind = stdlen + 1;
  794.       sawdst = TRUE;
  795.     } else {
  796.       oldfix = stdfix;
  797.       stdfix =
  798.         sp->ttis[i].tt_gmtoff + stdoffset;
  799.       if (sawstd && (oldfix != stdfix))
  800.         return -1;
  801.       sp->ttis[i].tt_gmtoff = -stdoffset;
  802.       sp->ttis[i].tt_abbrind = 0;
  803.       sawstd = TRUE;
  804.     }
  805.       }
  806.       /*
  807.        ** Make sure we have both standard and summer time.
  808.        */
  809.       if (!sawdst || !sawstd)
  810.     return -1;
  811.       /*
  812.        ** Now correct the transition times by shifting
  813.        ** them by the difference between the real and
  814.        ** prototype offsets.  Note that this difference
  815.        ** can be different in standard and summer time;
  816.        ** the prototype probably has a 1-hour difference
  817.        ** between standard and summer time, but a different
  818.        ** difference can be specified in TZ.
  819.        */
  820.       isdst = FALSE;        /* we start in standard time */
  821.       for (i = 0; i < sp->timecnt; ++i) {
  822.     register const struct ttinfo *    ttisp;
  823.  
  824.     /*
  825.      ** If summer time is in effect, and the
  826.      ** transition time was not specified as
  827.      ** standard time, add the summer time
  828.      ** offset to the transition time;
  829.      ** otherwise, add the standard time offset
  830.      ** to the transition time.
  831.      */
  832.     ttisp = &sp->ttis[sp->types[i]];
  833.     sp->ats[i] +=
  834.       (isdst && !ttisp->tt_ttisstd) ?
  835.         dstfix : stdfix;
  836.     isdst = ttisp->tt_isdst;
  837.       }
  838.     }
  839.   } else {
  840.     dstlen = 0;
  841.     sp->typecnt = 1;        /* only standard time */
  842.     sp->timecnt = 0;
  843.     sp->ttis[0].tt_gmtoff = -stdoffset;
  844.     sp->ttis[0].tt_isdst = 0;
  845.     sp->ttis[0].tt_abbrind = 0;
  846.   }
  847.   sp->charcnt = stdlen + 1;
  848.   if (dstlen != 0)
  849.     sp->charcnt += dstlen + 1;
  850.   if (sp->charcnt > sizeof sp->chars)
  851.     return -1;
  852.   cp = sp->chars;
  853.   (void) strncpy(cp, stdname, stdlen);
  854.   cp += stdlen;
  855.   *cp++ = '\0';
  856.   if (dstlen != 0) {
  857.     (void) strncpy(cp, dstname, dstlen);
  858.     *(cp + dstlen) = '\0';
  859.   }
  860.   return 0;
  861. }
  862.  
  863. static void
  864. gmtload(sp)
  865. struct state * const    sp;
  866. {
  867.     if (tzload(GMT, sp) != 0)
  868.         (void) tzparse(GMT, sp, TRUE);
  869. }
  870.  
  871. void
  872. tzset()
  873. {
  874.   register const char *    name;
  875.   void tzsetwall();
  876.  
  877.   name = getenv("TZ");
  878.   if (name == NULL) {
  879. #if 0
  880.     tzsetwall();
  881.     return;
  882. #else
  883.     name = "EST5";
  884. #endif
  885.   }
  886.   lcl_is_set = TRUE;
  887. #ifdef ALL_STATE
  888.   if (lclptr == NULL) {
  889.     lclptr = (struct state *) malloc(sizeof *lclptr);
  890.     if (lclptr == NULL) {
  891.       settzname();        /* all we can do */
  892.       __gettimeofday_init();
  893.       return;
  894.     }
  895.   }
  896. #endif /* defined ALL_STATE */
  897.   if (*name == '\0') {
  898.     /*
  899.      ** User wants it fast rather than right.
  900.      */
  901.     lclptr->leapcnt = 0;    /* so, we're off a little */
  902.     lclptr->timecnt = 0;
  903.     lclptr->ttis[0].tt_gmtoff = 0;
  904.     lclptr->ttis[0].tt_abbrind = 0;
  905.     (void) strcpy(lclptr->chars, GMT);
  906.   } else if (tzload(name, lclptr) != 0)
  907.     if (name[0] == ':' || tzparse(name, lclptr, FALSE) != 0)
  908.       (void) gmtload(lclptr);
  909.   settzname();
  910.   __gettimeofday_init();
  911. }
  912.  
  913. void
  914. tzsetwall()
  915. {
  916.     lcl_is_set = TRUE;
  917. #ifdef ALL_STATE
  918.     if (lclptr == NULL) {
  919.         lclptr = (struct state *) malloc(sizeof *lclptr);
  920.         if (lclptr == NULL) {
  921.             settzname();    /* all we can do */
  922.             return;
  923.         }
  924.     }
  925. #endif /* defined ALL_STATE */
  926.     if (tzload((char *) NULL, lclptr) != 0)
  927.         gmtload(lclptr);
  928.     settzname();
  929. }
  930.  
  931. /*
  932. ** The easy way to behave "as if no library function calls" localtime
  933. ** is to not call it--so we drop its guts into "localsub", which can be
  934. ** freely called.  (And no, the PANS doesn't require the above behavior--
  935. ** but it *is* desirable.)
  936. **
  937. ** The unused offset argument is for the benefit of mktime variants.
  938. */
  939.  
  940. /*ARGSUSED*/
  941. static void
  942. localsub(timep, offset, tmp)
  943. const time_t * const    timep;
  944. const long        offset;
  945. struct tm * const    tmp;
  946. {
  947.     register const struct state *    sp;
  948.     register const struct ttinfo *    ttisp;
  949.     register int            i;
  950.     const time_t            t = *timep;
  951.  
  952.     if (!lcl_is_set)
  953.         tzset();
  954.     sp = lclptr;
  955. #ifdef ALL_STATE
  956.     if (sp == NULL) {
  957.         gmtsub(timep, offset, tmp);
  958.         return;
  959.     }
  960. #endif /* defined ALL_STATE */
  961.     if (sp->timecnt == 0 || t < sp->ats[0]) {
  962.         i = 0;
  963.         while (sp->ttis[i].tt_isdst)
  964.             if (++i >= sp->typecnt) {
  965.                 i = 0;
  966.                 break;
  967.             }
  968.     } else {
  969.         for (i = 1; i < sp->timecnt; ++i)
  970.             if (t < sp->ats[i])
  971.                 break;
  972.         i = sp->types[i - 1];
  973.     }
  974.     ttisp = &sp->ttis[i];
  975.     /*
  976.     ** To get (wrong) behavior that's compatible with System V Release 2.0
  977.     ** you'd replace the statement below with
  978.     **    t += ttisp->tt_gmtoff;
  979.     **    timesub(&t, 0L, sp, tmp);
  980.     */
  981.     timesub(&t, ttisp->tt_gmtoff, sp, tmp);
  982.     tmp->tm_isdst = ttisp->tt_isdst;
  983.     tzname[tmp->tm_isdst] = (char *) &sp->chars[ttisp->tt_abbrind];
  984.     tmp->tm_zone = &sp->chars[ttisp->tt_abbrind];
  985. }
  986.  
  987. struct tm *
  988. localtime(timep)
  989. const time_t * const    timep;
  990. {
  991.     static struct tm    tm;
  992.  
  993.     localsub(timep, 0L, &tm);
  994.     return &tm;
  995. }
  996.  
  997. /*
  998. ** gmtsub is to gmtime as localsub is to localtime.
  999. */
  1000.  
  1001. static void
  1002. gmtsub(timep, offset, tmp)
  1003. const time_t * const    timep;
  1004. const long        offset;
  1005. struct tm * const    tmp;
  1006. {
  1007.     if (!gmt_is_set) {
  1008.         gmt_is_set = TRUE;
  1009. #ifdef ALL_STATE
  1010.         gmtptr = (struct state *) malloc(sizeof *gmtptr);
  1011.         if (gmtptr != NULL)
  1012. #endif /* defined ALL_STATE */
  1013.             gmtload(gmtptr);
  1014.     }
  1015.     timesub(timep, offset, gmtptr, tmp);
  1016.     /*
  1017.     ** Could get fancy here and deliver something such as
  1018.     ** "GMT+xxxx" or "GMT-xxxx" if offset is non-zero,
  1019.     ** but this is no time for a treasure hunt.
  1020.     */
  1021.     if (offset != 0)
  1022.         tmp->tm_zone = WILDABBR;
  1023.     else {
  1024. #ifdef ALL_STATE
  1025.         if (gmtptr == NULL)
  1026.             tmp->TM_ZONE = GMT;
  1027.         else    tmp->TM_ZONE = gmtptr->chars;
  1028. #endif /* defined ALL_STATE */
  1029. #ifndef ALL_STATE
  1030.         tmp->tm_zone = gmtptr->chars;
  1031. #endif /* State Farm */
  1032.     }
  1033. }
  1034.  
  1035. struct tm *
  1036. gmtime(timep)
  1037. const time_t * const    timep;
  1038. {
  1039.     static struct tm    tm;
  1040.  
  1041.     gmtsub(timep, 0L, &tm);
  1042.     return &tm;
  1043. }
  1044.  
  1045. static void
  1046. timesub(timep, offset, sp, tmp)
  1047. const time_t * const            timep;
  1048. const long                offset;
  1049. register const struct state * const    sp;
  1050. register struct tm * const        tmp;
  1051. {
  1052.     register const struct lsinfo *    lp;
  1053.     register long            days;
  1054.     register long            rem;
  1055.     register int            y;
  1056.     register int            yleap;
  1057.     register const int *        ip;
  1058.     register long            corr;
  1059.     register int            hit;
  1060.     register int            i;
  1061.  
  1062.     corr = 0;
  1063.     hit = FALSE;
  1064. #ifdef ALL_STATE
  1065.     i = (sp == NULL) ? 0 : sp->leapcnt;
  1066. #endif /* defined ALL_STATE */
  1067. #ifndef ALL_STATE
  1068.     i = sp->leapcnt;
  1069. #endif /* State Farm */
  1070.     while (--i >= 0) {
  1071.         lp = &sp->lsis[i];
  1072.         if (*timep >= lp->ls_trans) {
  1073.             if (*timep == lp->ls_trans)
  1074.                 hit = ((i == 0 && lp->ls_corr > 0) ||
  1075.                     lp->ls_corr > sp->lsis[i - 1].ls_corr);
  1076.             corr = lp->ls_corr;
  1077.             break;
  1078.         }
  1079.     }
  1080.     days = *timep / SECSPERDAY;
  1081.     rem = *timep % SECSPERDAY;
  1082. #ifdef mc68k
  1083.     if (*timep == 0x80000000) {
  1084.         /*
  1085.         ** A 3B1 muffs the division on the most negative number.
  1086.         */
  1087.         days = -24855;
  1088.         rem = -11648;
  1089.     }
  1090. #endif /* mc68k */
  1091.     rem += (offset - corr);
  1092.     while (rem < 0) {
  1093.         rem += SECSPERDAY;
  1094.         --days;
  1095.     }
  1096.     while (rem >= SECSPERDAY) {
  1097.         rem -= SECSPERDAY;
  1098.         ++days;
  1099.     }
  1100.     tmp->tm_hour = (int) (rem / SECSPERHOUR);
  1101.     rem = rem % SECSPERHOUR;
  1102.     tmp->tm_min = (int) (rem / SECSPERMIN);
  1103.     tmp->tm_sec = (int) (rem % SECSPERMIN);
  1104.     if (hit)
  1105.         /*
  1106.         ** A positive leap second requires a special
  1107.         ** representation.  This uses "... ??:59:60".
  1108.         */
  1109.         ++(tmp->tm_sec);
  1110.     tmp->tm_wday = (int) ((EPOCH_WDAY + days) % DAYSPERWEEK);
  1111.     if (tmp->tm_wday < 0)
  1112.         tmp->tm_wday += DAYSPERWEEK;
  1113.     y = EPOCH_YEAR;
  1114.     if (days >= 0)
  1115.         for ( ; ; ) {
  1116.             yleap = isleap(y);
  1117.             if (days < (long) year_lengths[yleap])
  1118.                 break;
  1119.             ++y;
  1120.             days = days - (long) year_lengths[yleap];
  1121.         }
  1122.     else do {
  1123.         --y;
  1124.         yleap = isleap(y);
  1125.         days = days + (long) year_lengths[yleap];
  1126.     } while (days < 0);
  1127.     tmp->tm_year = y - TM_YEAR_BASE;
  1128.     tmp->tm_yday = (int) days;
  1129.     ip = mon_lengths[yleap];
  1130.     for (tmp->tm_mon = 0; days >= (long) ip[tmp->tm_mon]; ++(tmp->tm_mon))
  1131.         days = days - (long) ip[tmp->tm_mon];
  1132.     tmp->tm_mday = (int) (days + 1);
  1133.     tmp->tm_isdst = 0;
  1134.     tmp->tm_gmtoff = offset;
  1135. }
  1136.  
  1137. /*
  1138. ** A la X3J11
  1139. */
  1140.  
  1141. char *
  1142. asctime(timeptr)
  1143. register const struct tm *    timeptr;
  1144. {
  1145.     static const char    wday_name[DAYSPERWEEK][3] = {
  1146.         "Sun", "Mon", "Tue", "Wed", "Thu", "Fri", "Sat"
  1147.     };
  1148.     static const char    mon_name[MONSPERYEAR][3] = {
  1149.         "Jan", "Feb", "Mar", "Apr", "May", "Jun",
  1150.         "Jul", "Aug", "Sep", "Oct", "Nov", "Dec"
  1151.     };
  1152.     static char    result[26];
  1153.  
  1154.     (void) sprintf(result, "%.3s %.3s%3d %02.2d:%02.2d:%02.2d %d\n",
  1155.         wday_name[timeptr->tm_wday],
  1156.         mon_name[timeptr->tm_mon],
  1157.         timeptr->tm_mday, timeptr->tm_hour,
  1158.         timeptr->tm_min, timeptr->tm_sec,
  1159.         TM_YEAR_BASE + timeptr->tm_year);
  1160.     return result;
  1161. }
  1162.  
  1163. char *
  1164. ctime(timep)
  1165. const time_t * const    timep;
  1166. {
  1167.     return asctime(localtime(timep));
  1168. }
  1169.  
  1170. /*
  1171. ** Adapted from code provided by Robert Elz, who writes:
  1172. **    The "best" way to do mktime I think is based on an idea of Bob
  1173. **    Kridle's (so its said...) from a long time ago. (mtxinu!kridle now).
  1174. **    It does a binary search of the time_t space.  Since time_t's are
  1175. **    just 32 bits, its a max of 32 iterations (even at 64 bits it
  1176. **    would still be very reasonable).
  1177. */
  1178.  
  1179. #ifndef WRONG
  1180. #define WRONG    (-1)
  1181. #endif /* !defined WRONG */
  1182.  
  1183. static void
  1184. normalize(tensptr, unitsptr, base)
  1185. int * const    tensptr;
  1186. int * const    unitsptr;
  1187. const int    base;
  1188. {
  1189.     if (*unitsptr >= base) {
  1190.         *tensptr += *unitsptr / base;
  1191.         *unitsptr %= base;
  1192.     } else if (*unitsptr < 0) {
  1193.         --*tensptr;
  1194.         *unitsptr += base;
  1195.         if (*unitsptr < 0) {
  1196.             *tensptr -= 1 + (-*unitsptr) / base;
  1197.             *unitsptr = base - (-*unitsptr) % base;
  1198.         }
  1199.     }
  1200. }
  1201.  
  1202. static int
  1203. tmcomp(atmp, btmp)
  1204. register const struct tm * const atmp;
  1205. register const struct tm * const btmp;
  1206. {
  1207.     register int    result;
  1208.  
  1209.     if ((result = (atmp->tm_year - btmp->tm_year)) == 0 &&
  1210.         (result = (atmp->tm_mon - btmp->tm_mon)) == 0 &&
  1211.         (result = (atmp->tm_mday - btmp->tm_mday)) == 0 &&
  1212.         (result = (atmp->tm_hour - btmp->tm_hour)) == 0 &&
  1213.         (result = (atmp->tm_min - btmp->tm_min)) == 0)
  1214.             result = atmp->tm_sec - btmp->tm_sec;
  1215.     return result;
  1216. }
  1217.  
  1218. static time_t
  1219. time2(tmp, funcp, offset, okayp)
  1220. struct tm * const    tmp;
  1221. void (* const        funcp)();
  1222. const long        offset;
  1223. int * const        okayp;
  1224. {
  1225.     register const struct state *    sp;
  1226.     register int            dir;
  1227.     register int            bits;
  1228.     register int            i, j ;
  1229.     register int            saved_seconds;
  1230.     time_t                newt;
  1231.     time_t                t;
  1232.     struct tm            yourtm, mytm;
  1233.  
  1234.     *okayp = FALSE;
  1235.     yourtm = *tmp;
  1236.     if (yourtm.tm_sec >= SECSPERMIN + 2 || yourtm.tm_sec < 0)
  1237.         normalize(&yourtm.tm_min, &yourtm.tm_sec, SECSPERMIN);
  1238.     normalize(&yourtm.tm_hour, &yourtm.tm_min, MINSPERHOUR);
  1239.     normalize(&yourtm.tm_mday, &yourtm.tm_hour, HOURSPERDAY);
  1240.     normalize(&yourtm.tm_year, &yourtm.tm_mon, MONSPERYEAR);
  1241.     while (yourtm.tm_mday <= 0) {
  1242.         --yourtm.tm_year;
  1243.         yourtm.tm_mday +=
  1244.             year_lengths[isleap(yourtm.tm_year + TM_YEAR_BASE)];
  1245.     }
  1246.     for ( ; ; ) {
  1247.         i = mon_lengths[isleap(yourtm.tm_year +
  1248.             TM_YEAR_BASE)][yourtm.tm_mon];
  1249.         if (yourtm.tm_mday <= i)
  1250.             break;
  1251.         yourtm.tm_mday -= i;
  1252.         if (++yourtm.tm_mon >= MONSPERYEAR) {
  1253.             yourtm.tm_mon = 0;
  1254.             ++yourtm.tm_year;
  1255.         }
  1256.     }
  1257.     saved_seconds = yourtm.tm_sec;
  1258.     yourtm.tm_sec = 0;
  1259.     /*
  1260.     ** Calculate the number of magnitude bits in a time_t
  1261.     ** (this works regardless of whether time_t is
  1262.     ** signed or unsigned, though lint complains if unsigned).
  1263.     */
  1264.     for (bits = 0, t = 1; t > 0; ++bits, t <<= 1)
  1265.         ;
  1266.     /*
  1267.     ** If time_t is signed, then 0 is the median value,
  1268.     ** if time_t is unsigned, then 1 << bits is median.
  1269.     */
  1270.     t = (t < 0) ? 0 : ((time_t) 1 << bits);
  1271.     for ( ; ; ) {
  1272.         (*funcp)(&t, offset, &mytm);
  1273.         dir = tmcomp(&mytm, &yourtm);
  1274.         if (dir != 0) {
  1275.             if (bits-- < 0)
  1276.                 return WRONG;
  1277.             if (bits < 0)
  1278.                 --t;
  1279.             else if (dir > 0)
  1280.                 t -= (time_t) 1 << bits;
  1281.             else    t += (time_t) 1 << bits;
  1282.             continue;
  1283.         }
  1284.         if (yourtm.tm_isdst < 0 || mytm.tm_isdst == yourtm.tm_isdst)
  1285.             break;
  1286.         /*
  1287.         ** Right time, wrong type.
  1288.         ** Hunt for right time, right type.
  1289.         ** It's okay to guess wrong since the guess
  1290.         ** gets checked.
  1291.         */
  1292.         sp = (const struct state *)
  1293.             ((funcp == localsub) ? lclptr : gmtptr);
  1294. #ifdef ALL_STATE
  1295.         if (sp == NULL)
  1296.             return WRONG;
  1297. #endif /* defined ALL_STATE */
  1298.         for (i = 0; i < sp->typecnt; ++i) {
  1299.             if (sp->ttis[i].tt_isdst != yourtm.tm_isdst)
  1300.                 continue;
  1301.             for (j = 0; j < sp->typecnt; ++j) {
  1302.                 if (sp->ttis[j].tt_isdst == yourtm.tm_isdst)
  1303.                     continue;
  1304.                 newt = t + sp->ttis[j].tt_gmtoff -
  1305.                     sp->ttis[i].tt_gmtoff;
  1306.                 (*funcp)(&newt, offset, &mytm);
  1307.                 if (tmcomp(&mytm, &yourtm) != 0)
  1308.                     continue;
  1309.                 if (mytm.tm_isdst != yourtm.tm_isdst)
  1310.                     continue;
  1311.                 /*
  1312.                 ** We have a match.
  1313.                 */
  1314.                 t = newt;
  1315.                 goto label;
  1316.             }
  1317.         }
  1318.         return WRONG;
  1319.     }
  1320. label:
  1321.     t += saved_seconds;
  1322.     (*funcp)(&t, offset, tmp);
  1323.     *okayp = TRUE;
  1324.     return t;
  1325. }
  1326.  
  1327. static time_t
  1328. time1(tmp, funcp, offset)
  1329. struct tm * const    tmp;
  1330. void (* const        funcp)();
  1331. const long        offset;
  1332. {
  1333.     register time_t            t;
  1334.     register const struct state *    sp;
  1335.     register int            samei, otheri;
  1336.     int                okay;
  1337.  
  1338.     if (tmp->tm_isdst > 1)
  1339.         tmp->tm_isdst = 1;
  1340.     t = time2(tmp, funcp, offset, &okay);
  1341.     if (okay || tmp->tm_isdst < 0)
  1342.         return t;
  1343.     /*
  1344.     ** We're supposed to assume that somebody took a time of one type
  1345.     ** and did some math on it that yielded a "struct tm" that's bad.
  1346.     ** We try to divine the type they started from and adjust to the
  1347.     ** type they need.
  1348.     */
  1349.     sp = (const struct state *) ((funcp == localsub) ? lclptr : gmtptr);
  1350. #ifdef ALL_STATE
  1351.     if (sp == NULL)
  1352.         return WRONG;
  1353. #endif /* defined ALL_STATE */
  1354.     for (samei = 0; samei < sp->typecnt; ++samei) {
  1355.         if (sp->ttis[samei].tt_isdst != tmp->tm_isdst)
  1356.             continue;
  1357.         for (otheri = 0; otheri < sp->typecnt; ++otheri) {
  1358.             if (sp->ttis[otheri].tt_isdst == tmp->tm_isdst)
  1359.                 continue;
  1360.             tmp->tm_sec += sp->ttis[otheri].tt_gmtoff -
  1361.                     sp->ttis[samei].tt_gmtoff;
  1362.             tmp->tm_isdst = !tmp->tm_isdst;
  1363.             t = time2(tmp, funcp, offset, &okay);
  1364.             if (okay)
  1365.                 return t;
  1366.             tmp->tm_sec -= sp->ttis[otheri].tt_gmtoff -
  1367.                     sp->ttis[samei].tt_gmtoff;
  1368.             tmp->tm_isdst = !tmp->tm_isdst;
  1369.         }
  1370.     }
  1371.     return WRONG;
  1372. }
  1373.  
  1374. time_t
  1375. mktime(tmp)
  1376. const struct tm * tmp;
  1377. {
  1378.     return time1(tmp, localsub, 0L);
  1379. }
  1380.